package com.example.chirdrenserver.consumer;

import cn.hutool.core.util.ObjectUtil;
import com.example.chirdrenserver.Constant.RedisConstant;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.entity.PostContentMessage;
import com.example.chirdrenserver.Pojo.entity.PostFileMessage;
import com.example.chirdrenserver.Pojo.entity.PostMessage;
import com.example.chirdrenserver.Service.UserService.PostService;
import com.example.chirdrenserver.confg.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author Yu
 * @Description //TODO $
 * @Date $ $
 **/
@Component
@Slf4j
public class postConsumer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private PostService postService;

    @Autowired
    RedisTemplate redisTemplate;

    @RabbitListener(queues = RabbitMQConfig.POST_SEND_QUEUE)
    public void handleMessage(Message message, Channel channel) throws IOException {
        String taskId = (String) message.getMessageProperties().getHeaders().get("taskId");
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        //缓存消息
        if (routingKey.equals(RabbitMQConfig.POST_SEND_ROUTING_KEY)) {
            if (ObjectUtil.isEmpty(redisTemplate.opsForSet().isMember(RedisConstant.MESSAGE_TASK_ID, taskId))) {
                long deliveryTag = message.getMessageProperties().getDeliveryTag();
                try {
                    processMessage(message);
                    redisTemplate.opsForSet().add(RedisConstant.MESSAGE_TASK_ID + taskId, taskId);
                    // 手动确认消息
                    channel.basicAck(deliveryTag, false);
                } catch (Exception e) {
                    log.error("处理消息失败: taskId={}, routingKey={}", taskId, routingKey, e);
                    channel.basicReject(deliveryTag, false); // 丢弃或进入死信队列
//             }
                }
            }
        }
        //文件消息
    }


    @RabbitListener(queues = RabbitMQConfig.POST_SEND_QUEUE)
    public void handleAddFileMessage(Message message, Channel channel) throws IOException {
        String taskId = (String) message.getMessageProperties().getHeaders().get("taskId");
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        if (!redisTemplate.opsForSet().isMember(RedisConstant.MESSAGE_TASK_ID, taskId)) {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            try {
                processAddFileMessage(message);
                redisTemplate.opsForSet().add(RedisConstant.MESSAGE_TASK_ID + taskId, taskId);
                // 手动确认消息
                channel.basicAck(deliveryTag, false);
            } catch (Exception e) {
                log.error("处理消息失败: taskId={}, routingKey={}", taskId, routingKey, e);
                channel.basicReject(deliveryTag, false); // 丢弃或进入死信队列
//             }

            }

        }
    }

    @RabbitListener(queues = RabbitMQConfig.POST_ADD_CONTENT_QUEUE)
    public void handleAddContentMessage(Message message, Channel channel) throws IOException {
        String taskId = (String) message.getMessageProperties().getHeaders().get("taskId");
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        //纯文本消息

        if (!redisTemplate.opsForSet().isMember(RedisConstant.MESSAGE_TASK_ID, taskId)) {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            try {
                processAddContentMessage(message);
                redisTemplate.opsForSet().add(RedisConstant.MESSAGE_TASK_ID + taskId, taskId);
                // 手动确认消息
                channel.basicAck(deliveryTag, false);
            } catch (Exception e) {
                log.error("处理消息失败: taskId={}, routingKey={}", taskId, routingKey, e);
                channel.basicReject(deliveryTag, false); // 丢弃或进入死信队列
            }
        }
    }

    private boolean processMessage(Message message) {
        PostMessage postMessage = (PostMessage) rabbitTemplate.getMessageConverter().fromMessage(message);
        BasePage basePage = new BasePage();
        basePage.setPage(postMessage.getPage());
        basePage.setLimit(postMessage.getLimit());
        postService.getPost(basePage);
        return true;
    }

    private boolean processAddFileMessage(Message message) {
        PostFileMessage postFileMessage = (PostFileMessage) rabbitTemplate.getMessageConverter().fromMessage(message);
        postService.addFilePost(postFileMessage);
        return true;
    }

    private boolean processAddContentMessage(Message message) {
        PostContentMessage postContentMessage = (PostContentMessage) rabbitTemplate.getMessageConverter().fromMessage(message);

        postService.addContent(postContentMessage);
        return true;
    }

}
